package com.zpy.playground.redisTest;

import com.alibaba.fastjson.JSON;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    @Qualifier("general")
    private Cache<Object, Object> generalCache;

    @Autowired
    @Qualifier("normal")
    private Cache<String, Object> normalCache;

    @Autowired
    @Qualifier("specify")
    private Cache<Integer, NestB> specifyCache;

    @Autowired
    private RedisUtil redisUtil;

    public static final String CHATTING_BROADCASTER_WALL_REALTIME_PICKUP_RATE_LIST = "chatting:broadcaster:wall:realtime:pickup:rate:list";

    @PostMapping("/in1")
    public void inMemory() {
        List<NestA> aList = new ArrayList<>();
        for (int i = 0; i < 10; ++i) {
            NestA a = new NestA("a" + i, i);
            aList.add(a);
        }

        List<NestB> bList = new ArrayList<>();
        for (int i = 0; i < 10; ++i) {
            NestB b = new NestB(aList.get(i), "b" + i, i);
            bList.add(b);
        }
        Map<String, NestA> aMap = aList.stream().collect(Collectors.toMap(NestA::getAStr, Function.identity()));
        Map<Integer, NestB> bMap = bList.stream().collect(Collectors.toMap(NestB::getBint, Function.identity()));

        NestA a1 = new NestA("a" + 1, 1);
        NestB b1 = new NestB(a1, "b", 2);

        specifyCache.put(b1.getBint(), b1);

        normalCache.put(a1.getAStr(), a1);
        normalCache.put(b1.getBint().toString(), b1);

        Object aCache = normalCache.getIfPresent(b1.getBStr());
        NestB bCache = specifyCache.getIfPresent(b1.getBint());


        NestB b12 = (NestB) normalCache.getIfPresent(b1.getBint().toString());
        System.out.println(b12.getBint());

        normalCache.put("bMap", bMap);
        normalCache.put("aMap", aMap);

        Map<Integer, NestB> bCacheMap = (Map<Integer, NestB>) normalCache.getIfPresent("bMap");
        NestB nestB = bCacheMap.get(1);
        System.out.println(nestB);
        System.out.println();

        generalCache.put(12345, bMap);
        Map<Integer, NestB> generalMap1 = (Map<Integer, NestB>) generalCache.getIfPresent(12345);
        NestB gB1 = generalMap1.get(1);
        System.out.println(gB1);
        System.out.println();

        Integer i = 12345;
        generalCache.put(i, bMap);
        Object p1 = generalCache.getIfPresent(12345);
        Object p2 = generalCache.getIfPresent(i);
        System.out.println();
    }

    @PostMapping("/redisT3")
    public void t3() {
        Object obj = redisUtil.hget(CHATTING_BROADCASTER_WALL_REALTIME_PICKUP_RATE_LIST);
        redisUtil.set("123", 123, 60);
        Object o = redisUtil.get("123");
        System.out.println();
    }
}
